#ifndef DESKTOPTIMER_H
#define DESKTOPTIMER_H

#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include "view_global.h"
#include <QVBoxLayout>
#include "RoundTimeBar.h"
#include "DigitalClock.h"
#include "DateLCD.h"
#include "WeekdayDisplay.h"
#include "SettingWidget.h"
#include "FramelessWindow.h"
#include <QSystemTrayIcon> //托盘使用的头文件
#include <QShortcut>
#include "ScrollingLabel.h"
#include <QPointer>
#include <QHash>
#include <functional>

class ScreenshotWidget;
class TaskPanelController;
class PlanWidget;
class QPropertyAnimation;
class QFrame;
class QVBoxLayout;

// [FDD-2025-03-021] WindowRegistry for singleton window management
struct WindowRegistryEntry {
    QString key;                            // 唯一标识符
    QPointer<QWidget> widget;               // 自动失效的窗口引用
    std::function<QWidget*()> factory;      // 延迟创建函数
};

class VIEW_EXPORT DesktopTimer : public FramelessWindow{
    Q_OBJECT
public:
    int getScreenWidth() const;
    void setScreenWidth(int width);
    int getScreenHeight() const;
    void setScreenHeigth(int heigth);
protected:
    void resizeEvent(QResizeEvent *event) override;
public:
    explicit DesktopTimer(QWidget *parent = nullptr);
    virtual ~DesktopTimer();
protected:

    void closeEvent(QCloseEvent * event);
    void changeEvent(QEvent* event) override;
    // 鼠标进入事件
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    void enterEvent(QEnterEvent *event) override;
#else
    void enterEvent(QEvent *event) override;
#endif
    // 鼠标离开事件
    void leaveEvent(QEvent *event) override;
private:
    void initForm();// 初始化窗口设置
    void initSystemTrayIcon();
    void applyStyles();                      // 加载样式表
private:
    void applyOpacityTransition(QWidget *widget, qreal startOpacity, qreal endOpacity, int duration);
    void setScreenStyle(ScreenAction action);
    void connectFunction();
    void setWindowStyle();                   // 设置窗口样式和背景
    void initMainLayout();                   // 初始化主布局
    void refreshLayoutGeometry();            // 刷新主布局和窗口几何
    void loadSettingsFromConfig();
    RoundTimeBar* createRoundTimeBar();      // 创建圆形进度条
    DigitalClock* createDigitalClock();      // 创建数字时钟
    DateLCD* createDateLCD();                // 创建日期显示
    WeekdayDisplay* createWeekdayDisplay();  // 创建星期显示
    ScrollingLabel* createLabel();                   // 创建带文本的标签
    QHBoxLayout* createButtonLayout(); // 创建按钮布局
    QPushButton* createButton(const QString& iconPath, const QSize& iconSize); // 创建按钮
    void setTitleBarVisible(bool visible);
    void onTitleAnimationFinished();

private slots:
    // 封装的动画函数
    void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
    // 打开设置界面
    void openSettings();
    // 打开日计划界面
    void openPlan();
    // 静音或取消静音
    void toggleMute();
    // 切换窗口置顶状态
    void togglePin();
    // 开始截图
    void startScreenshot();
    // 显示关于对话框
    void showAboutDialog();
    // 导出数据到文件
    void exportData();

    void updateAllData();

    // [FDD-2025-03-021][API-001] WindowRegistry 清理槽
    void onWindowDestroyed(QObject* obj);
    
    // [FDD-2025-03-015] 任务面板控制
    void onTaskPanelToggle();
    void onPanelAnimationFinished();
private:
    int m_nScreenWidth;
    int m_nScreenHeigth;
    int m_baseWindowHeight = 0;            // 基础窗口高度（任务面板收起时）
    bool m_bTitleShow;
    bool m_titleTargetVisible = true;
    QPushButton* m_pSoundBtn;
    QPushButton* m_pPinBtn;
    QPushButton* m_pPlanBtn;
    QPushButton* m_pScreenshotBtn;  // 截图按钮
    QPushButton* m_pSettingBtn;
    QPushButton* m_pTaskPanelBtn;  // [FDD-2025-03-015] 任务面板切换按钮
    QHBoxLayout *mainLayout;
    ScreenshotWidget* m_pScreenshotWidget;  // 截图窗口
    bool m_bSound;
    bool m_bPinned;  // 窗口是否置顶
    RoundTimeBar* roundTimeBar;
    QThread *roundTimeBarThread;
    DateLCD* m_pDate;
    ScrollingLabel* m_pLabel;
    DigitalClock* m_pOclock;
    WeekdayDisplay* m_pWeekday;
    
    // [FDD-2025-03-015] 任务面板组件
    QVBoxLayout* m_mainContainer = nullptr;      // 主容器（外层VBox）
    QFrame* m_taskPanelHost = nullptr;           // 任务面板容器
    TaskPanelController* m_taskPanelCtrl = nullptr;  // 控制器
    PlanWidget* m_embeddedPlanWidget = nullptr;  // 嵌入的紧凑PlanWidget
    QPropertyAnimation* m_panelAnimation = nullptr;  // 展开/收缩动画
    QShortcut* m_taskPanelShortcut = nullptr;    // Alt+T 快捷键
    
    bool m_taskPanelExpanded = false;            // 当前展开状态
    qint64 m_lastToggleTs = 0;                   // 防抖时间戳

private:
    // 标题栏
    QWidget* titleWidget;
    QLabel* lblIcon;
    QLabel* lblTitle;
    QPushButton* btnClose;
    QPushButton* btnMax;
    QPushButton* btnMin;
    QPropertyAnimation* m_titleAnimation = nullptr;

    // 主体
    QWidget* mainWidget;

private:
    QSystemTrayIcon *m_pSysIcon;  // 托盘图标
    QMenu *trayMenu;              // 托盘菜单
    QAction *muteAction;          // 静音/取消静音菜单项
    QAction *unlockAction;        // 解除锁定菜单项
    QAction *settingsAction;
    QAction *refreshAction;
    QAction *aboutAction;
    QAction *exportDataAction;
    QAction *quitAction;
    QShortcut *unlockShortcut;    // 解除锁定快捷键

    // [FDD-2025-03-021] WindowRegistry 窗口注册表
    QHash<QString, WindowRegistryEntry> m_windowRegistry;

private:
    // [API-001][FDD-2025-03-015] 切换任务面板
    void toggleTaskPanel(bool expanded);

    // [FDD-2025-03-015] 创建任务面板容器
    void createTaskPanelHost(QVBoxLayout* parentLayout);

    // [FDD-2025-03-021][API-001] WindowRegistry 单例窗口管理
    QWidget* showSingletonWindow(const QString& key, std::function<QWidget*()> factory);
};

#endif // DESKTOPTIMER_H
